//
//  MyOrderViewController.m
//  EarnCommision
//
//  Created by Nancy on 3/15/15.
//  Copyright (c) 2015 EC. All rights reserved.
//

#import "MyOrderViewController.h"
#import "UIViewExt.h"

#define kScreenHeight [UIScreen mainScreen].bounds.size.height
#define kScreenWidth [UIScreen mainScreen].bounds.size.width

@interface MyOrderViewController ()

@property (nonatomic,strong) UIButton *allOrder;
@property (nonatomic,strong) UIButton *inprocessOrder;
@property (nonatomic,strong) UIButton *releaseOrder;
@property (nonatomic,strong) UIButton *successOrder;
@property (nonatomic,strong) UIButton *failOrder;
@property (nonatomic,strong) UIButton  *myDistribute;
@property (nonatomic,strong) UIButton  *myGrab;

@end

@implementation MyOrderViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    float buttonWidth = kScreenWidth/5;
    float buttonHeight = buttonWidth;
    
    self.title = @"我的订单";
    [self.view setBackgroundColor:[UIColor colorWithRed:240/255.0 green:240/255.0 blue:240/255.0 alpha:1.0]];
    [self.view setBackgroundColor:[UIColor whiteColor]];
    
    _myDistribute = [[UIButton alloc]initWithFrame:CGRectMake(0, 66, kScreenWidth/2, 50)];
    [_myDistribute setTitle:@"我发布的" forState:UIControlStateNormal];
    [_myDistribute setTitle:@"我发布的" forState:UIControlStateSelected];
    [_myDistribute setTitleColor:[UIColor blueColor] forState:UIControlStateSelected];
    [_myDistribute setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [_myDistribute setSelected:YES];
    [_myDistribute addTarget:self action:@selector(distributeBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_myDistribute];
    
    _myGrab = [[UIButton alloc]initWithFrame:CGRectMake(_myDistribute.right, 66, kScreenWidth/2, 50)];
    [_myGrab setTitle:@"我抢的" forState:UIControlStateNormal];
    [_myGrab setTitle:@"我抢的" forState:UIControlStateSelected];
    [_myGrab setTitleColor:[UIColor blueColor] forState:UIControlStateSelected];
    [_myGrab setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [_myGrab addTarget:self action:@selector(grabBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_myGrab];
    
    _allOrder = [[UIButton alloc]initWithFrame:CGRectMake(0, kScreenHeight-buttonHeight, buttonWidth, buttonHeight)];
    [_allOrder setBackgroundImage:[UIImage imageNamed:@"order_all_normal"] forState:UIControlStateNormal];
    [_allOrder setBackgroundImage:[UIImage imageNamed:@"order_all_sel"] forState:UIControlStateSelected];
    [_allOrder addTarget:self action:@selector(allBtnClick) forControlEvents:UIControlEventTouchDown];
    [_allOrder setSelected:YES];
    
    _inprocessOrder = [[UIButton alloc]initWithFrame:CGRectMake(_allOrder.right, kScreenHeight-buttonHeight, buttonWidth, buttonHeight)];
    [_inprocessOrder setBackgroundImage:[UIImage imageNamed:@"order_process_normal"] forState:UIControlStateNormal];
    [_inprocessOrder setBackgroundImage:[UIImage imageNamed:@"order_process_sel"] forState:UIControlStateSelected];
    [_inprocessOrder addTarget:self action:@selector(inprocessBtnClick) forControlEvents:UIControlEventTouchDown];
    
    _releaseOrder = [[UIButton alloc]initWithFrame:CGRectMake(_inprocessOrder.right, kScreenHeight-buttonHeight, buttonWidth, buttonHeight)];
    [_releaseOrder setBackgroundImage:[UIImage imageNamed:@"order_release_normal"] forState:UIControlStateNormal];
    [_releaseOrder setBackgroundImage:[UIImage imageNamed:@"order_release_sel"] forState:UIControlStateSelected];
    [_releaseOrder addTarget:self action:@selector(releaseBtnClick) forControlEvents:UIControlEventTouchDown];
    
    _successOrder = [[UIButton alloc]initWithFrame:CGRectMake(_releaseOrder.right, kScreenHeight-buttonHeight, buttonWidth, buttonHeight)];
    [_successOrder setBackgroundImage:[UIImage imageNamed:@"order_success_normal"] forState:UIControlStateNormal];
    [_successOrder setBackgroundImage:[UIImage imageNamed:@"order_success_sel"] forState:UIControlStateSelected];
    [_successOrder addTarget:self action:@selector(successBtnClick) forControlEvents:UIControlEventTouchDown];
    
    _failOrder = [[UIButton alloc]initWithFrame:CGRectMake(_successOrder.right, kScreenHeight-buttonHeight, buttonWidth, buttonHeight)];
    [_failOrder setBackgroundImage:[UIImage imageNamed:@"order_fail_normal"] forState:UIControlStateNormal];
    [_failOrder setBackgroundImage:[UIImage imageNamed:@"order_fail_sel"] forState:UIControlStateSelected];
    [_failOrder addTarget:self action:@selector(failBtnClick) forControlEvents:UIControlEventTouchDown];
    
    [self.view addSubview:_allOrder];
    [self.view addSubview:_inprocessOrder];
    [self.view addSubview:_releaseOrder];
    [self.view addSubview:_successOrder];
    [self.view addSubview:_failOrder];
    
    m_tableView = [[UITableView alloc]initWithFrame:CGRectMake(0,66+_myDistribute.height, kScreenWidth, kScreenHeight-buttonHeight-66-_myDistribute.height)];
    [m_tableView setBackgroundColor:[UIColor clearColor]];
    [m_tableView setDelegate:self];
    [m_tableView setDataSource:self];
    //m_tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:m_tableView];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)setAllButtonNormal
{
    [_allOrder setSelected:NO];
    [_inprocessOrder setSelected:NO];
    [_releaseOrder setSelected:NO];
    [_successOrder setSelected:NO];
    [_failOrder setSelected:NO];
}

-(void)allBtnClick
{
    [self setAllButtonNormal];
    [_allOrder setSelected:YES];
}

-(void)inprocessBtnClick
{
    [self setAllButtonNormal];
    [_inprocessOrder setSelected:YES];
}

-(void)releaseBtnClick
{
    [self setAllButtonNormal];
    [_releaseOrder setSelected:YES];
}
-(void)successBtnClick
{
    [self setAllButtonNormal];
    [_successOrder setSelected:YES];
}
-(void)failBtnClick
{
    [self setAllButtonNormal];
    [_failOrder setSelected:YES];
}
-(void)distributeBtnClick
{
    [_myDistribute setSelected:YES];
    [_myGrab setSelected:NO];
}
-(void)grabBtnClick
{
    [_myDistribute setSelected:NO];
    [_myGrab setSelected:YES];
}
#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return 4;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell";
    
    MyOrderCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[MyOrderCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    
    cell.m_titleLabel.text = @"帮我去取一箱快递";
    cell.m_dataLabel.text =  @"2015.03.16";
    cell.m_priceLabel.text = @"102￥";
    cell.m_personCountLabel.text = @"0人抢单";
    
    UIImage *image = [UIImage imageNamed:@"latest_activities_image_1.png"];
    [cell setImageView:image];
    
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 70;
}

-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(editingStyle == UITableViewCellEditingStyleDelete)
    {
        NSUInteger row = [indexPath row];
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationBottom];
    }
}
@end

@implementation MyOrderCell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        _m_cellBackground = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth,70)];
        _m_imageView = [[UIImageView alloc]initWithFrame:CGRectMake(10, 20,40,30)];
        [_m_cellBackground addSubview:_m_imageView];
        
        _m_titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(_m_imageView.right+10, _m_imageView.top-10, _m_cellBackground.width-160, 20)];
        [_m_cellBackground addSubview:_m_titleLabel];
        
        _m_dataLabel = [[UILabel alloc]initWithFrame:CGRectMake(_m_titleLabel.left, _m_titleLabel.bottom+10, _m_cellBackground.width-160, 20)];
        [_m_cellBackground addSubview:_m_dataLabel];
        
        _m_priceLabel = [[UILabel alloc]initWithFrame:CGRectMake(_m_titleLabel.right+10, _m_titleLabel.top,80, 20)];
        [_m_cellBackground addSubview:_m_priceLabel];
        
        _m_personCountLabel = [[UILabel alloc]initWithFrame:CGRectMake(_m_priceLabel.left, _m_titleLabel.bottom+10,80, 20)];
        [_m_cellBackground addSubview:_m_personCountLabel];
        
        [self addSubview:_m_cellBackground];
        self.selectionStyle = UITableViewCellSelectionStyleGray;
    }
    return self;
}

- (void)setImageView:(UIImage*)image
{
    _m_imageView.image = image;

}

@end

